/**********************************************************
 * File description:samplelibrary.cpp
 * Function:样本库管理类
 * Current Version: 1.0.0
 * Author: 王伟豪
 * Date: 2022-10-02
 *  -----
 *  Copyright by BoKun Software.
 **********************************************************/
#include<SamplelibraryService.h>


SampleLibrary::SampleLibrary()
{
    path = QCoreApplication::applicationDirPath();
    sqlD = new SqliteDriver();
    sqlD->setDBDir(path);
    sqlD->openDB(DBNAME);
}

SampleLibrary::~SampleLibrary()
{
    sqlD->closeDB();
}


/*!
 * \brief 向SAMPLE中插入数据
 *
 * \param QString name, QString colorValue,
 * \param QString remarks
 *
 * Detail here!!!
 */
void SampleLibrary::create(QString name, QString colorValue, QString remarks)
{

    QHash<QString,QString> data;
    data["name"] = name;
    data["color_value"] = colorValue;
    data["remarks"] = remarks;
    sqlD->addData(SAMPLE,data); //插入操作
}



/*!
 * \brief 删除SAMPLE中某一条数据
 *
 * \param QString id   要删除数据的id号
 *
 * Detail here!!!
 */
void SampleLibrary::remove(QString id)
{
     sqlD->delData(SAMPLE,"where id = "+id); //删除操作
}



/*!
 * \brief 修改SAMPLE中某一条数据
 *
 * \param QString id,  要修改的数据的id
 * \param QString name, QString colorValue,
 * \param QString remarks
 *
 * Detail here!!!
 */
void SampleLibrary::revise(QString id,QString name, QString colorValue, QString remarks)
{

    QHash<QString,QString> data;
    data["name"] = name;
    data["color_value"] = colorValue;
    data["remarks"] = remarks;
    data["update_time"] = "DATETIME('now','localtime')";
    sqlD->updateData(SAMPLE,data,"where id = "+id);
}



/*!
 * \brief 用于获取SAMPLE中的全部数据
 *
 * \return QList<QHash<QString,QString>> 由qlist封装每一条数据（qhash）
 *
 * Detail here!!!
 */
QList<QHash<QString,QString>> SampleLibrary::show()
{
    QList<QHash<QString,QString>> data;
    sqlD->queryExec("SELECT * FROM sample ORDER BY id",data);

    return data;
}
